package daily.play2;
/*一共有1000张电影票，可以在两个窗口领取，假设每次领取的时间为3000毫秒，
要求：请用多线程模拟卖票过程并打印剩余电影票的数量*/
/*class Mythread extends Thread{
    public static int sum = 1000;
    public void run(){
        while(true){
            synchronized (Mythread.class){
                if(sum<=0){
                    break;
                }else{
                    sum--;
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(getName()+"买了一张票，还剩"+sum+"张");
                }
            }
        }
    }
}

public class demo3 {
    public static void main(String[] args){
        Mythread t1 = new Mythread();
        Mythread t2 = new Mythread();
        t1.setName("窗口1");
        t2.setName("窗口2");

        t1.start();
        t2.start();
    }
}*/

/*有100份礼品，两人同时发送，当剩下的礼品小于10份的时候则不再送出。
利用多线程模拟该过程并将线程的名字和礼物的剩余数量打印出来。*/
/*class MyThread extends Thread{
    public static int sum = 100;
    public void run(){
        while(true){
            synchronized (MyThread.class){
                if(sum<=10){
                    break;
                }else{
                    sum--;
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(getName()+"送出了一份礼物，还剩"+sum+"份礼物");
                }
            }
        }
    }
}
public class demo3{
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        t1.setName("经理1");
        t2.setName("经理2");
        t1.start();
        t2.start();
    }
}*/


/*同时开启两个线程，共同获取1-100之间的所有数字。
要求：将输出所有的奇数。*/
/*
class MyThread implements Runnable{
    public int sum = 0;
    public void run(){
        while(true){
            synchronized (MyThread.class){
                if(sum>=100){
                    break;
                }else{
                    sum++;
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    if(sum%2==1){
                        System.out.println(Thread.currentThread().getName()+"找到了奇数"+sum);
                    }
                }
            }
        }
    }
}
public class demo3{
    public static void main(String[] args){
        MyThread thread = new MyThread();
        Thread t1 = new Thread(thread);
        Thread t2 = new Thread(thread);
        t1.setName("学生1");
        t2.setName("学生2");
        t1.start();
        t2.start();
    }
}*/


import com.sun.jdi.event.ThreadDeathEvent;

import javax.swing.plaf.synth.SynthRadioButtonMenuItemUI;
import java.nio.channels.NonWritableChannelException;
import java.util.*;
import java.util.concurrent.*;

/*抢红包也用到了多线程
假设：100块，分成了3个包，现在有5个人去抢。
其中，红包是共享数据。
5个人是5条线程。
打印结果如下：
XXX抢到了XXX元
XXX抢到了XXX元
XXX抢到了XXX元
XXX没抢到
XXX没抢到*/
/*
class mythread implements Runnable{
    Random ran = new Random();
    double MIN = 0.01;
    int sum = 3;
    double money = 100;
    public void run(){
        synchronized(this){
            if(sum!=0){
                double bounds = money-(sum-1)*MIN;
                sum--;
                double temp = ran.nextDouble(bounds);
                System.out.println(Thread.currentThread().getName()+"抢到了"+temp+"元");
                money-=temp;
            }
        }

    }
}

public class demo3{
    public static void main(String[] args) {
        mythread thread = new mythread();
        Thread thread1 = new Thread(thread);
        Thread thread2 = new Thread(thread);
        Thread thread3 = new Thread(thread);
        Thread thread4 = new Thread(thread);
        Thread thread5 = new Thread(thread);
        thread1.setName("张三");
        thread2.setName("李四");
        thread3.setName("王五");
        thread4.setName("赵六");
        thread5.setName("宋七");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}*/



/*有一个抽奖池，该抽奖池中存放了奖励的金额，该抽奖池中的奖项为
{10,5,20,50,100,200,500,800,2,80,300,700};
创建两个抽奖箱（线程）设置线程名称分别为“抽奖箱1”，“抽奖箱2”
随机从抽奖池中获取奖项元素并打印在控制台上，格式如下：
每次抽出一个奖项就打印一个（随机）
抽奖箱1又产生了一个10元大奖
抽奖箱1又产生了一个100元大奖
抽奖箱1又产生了一个200元大奖
抽奖箱1又产生了一个800元大奖
抽奖箱2又产生了一个700元大奖*/

/*
class  mythread implements Runnable{
    ArrayList<Integer> list = new ArrayList<>();
    public mythread(ArrayList<Integer> list){
        this.list = list;
    }
    public void run(){
        while(true){
            synchronized (mythread.class){
                if(list.size()==0){
                    break;
                }else{
                    Collections.shuffle(list);

                    System.out.println(Thread.currentThread().getName()+"抽到了一个"+list.remove(0)+"元大奖");
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class demo3{
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList();
        Collections.addAll(list,10,5,20,50,300,500,1000);

        mythread mythread = new mythread(list);
        Thread t1 = new Thread(mythread,"抽奖箱1");
        Thread t2 = new Thread(mythread,"抽奖箱2");
        t2.setPriority(10);
        t1.setPriority(1);
        t2.start();
        t1.start();
    }
}*/


/*在上一题基础上继续完成如下需求：
每次抽的过程中，不打印，抽完时一次性打印（随机）
在此次抽奖过程中，抽奖箱1总共产生了6个奖项。
分别为：10,20,100,500,2,300最高奖项为300元，总计额为932元
在此次抽奖过程中，抽奖箱2总共产生了6个奖项。
分别为：5,50,200,800,80,700最高奖项为800元，总计额为1835元*/
/*
class Max {
    public static int getMax(ArrayList<Integer> list) {
        if (list.size() >= 1) {
            int a = list.get(0);
            for (Integer b : list) {
                if (b > a) {
                    a = b;
                }
            }
            return a;
        } else {
            return 0;
        }
    }
}
class mythread implements Runnable{
    ArrayList<Integer> list1 = new ArrayList<>();
    ArrayList<Integer> list2 = new ArrayList<>();
    int max;
    public mythread(ArrayList<Integer> list,int max){
        this.list1 = list;
        this.max = max;
    }
    public void run(){
        while(true){
            synchronized (mythread.class){
                if(list1.size()==0){
                    break;
                }else{
                    Collections.shuffle(list1);
                    list2.add(list1.remove(0));
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        print();
        if(list2.contains(max)){
            System.out.println(Thread.currentThread().getName()+"获得了最高奖项！");
        }
    }
    public  void print(){
        System.out.println(Thread.currentThread().getName()+"获得了"+list2);
        System.out.println(Thread.currentThread().getName()+"最高获得了"+Max.getMax(list2));
    }
}

public class demo3{
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,10,20,30,400,500,800,700,900,200);
        int max = Max.getMax(list);
        mythread thread1 = new mythread(list,max);
        mythread thread2 = new mythread(list,max);
        Thread t1 = new Thread(thread1,"张三");
        Thread t2 = new Thread(thread2,"李四");
        t1.start();
        t2.start();
    }
}*/

//线程池
/*class MyThread implements Runnable{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+"----------"+i);
        }
    }
}
public class demo3{
    public static void main(String[] args) {
        ExecutorService threadPools = Executors.newFixedThreadPool(2);
        threadPools.submit(new MyThread());
        threadPools.submit(new MyThread());
        threadPools.submit(new MyThread());
        threadPools.submit(new MyThread());
        threadPools.submit(new MyThread());
        //销毁线程池
        threadPools.shutdown();//停止接收新的任务
        System.out.println(1);
    }
}*/

//自定义线程池
/*class MyThread implements Runnable{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+"------"+i);
        }
    }
}
public class demo3{
    public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                6,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        pool.submit(new MyThread());
        pool.submit(new MyThread());
        pool.submit(new MyThread());
        pool.shutdown();
    }
}*/

//获取最大并行数
/*public class demo3{
    public static void main(String[] args){
        Runtime run = Runtime.getRuntime();
        System.out.println(run.availableProcessors());
    }
}*/

//查看电脑配置
/*public class demo3{
    public static void main(String[] args) {
        Properties pro = System.getProperties();
        Enumeration enumeration =pro.propertyNames();
        while(enumeration.hasMoreElements()){
            String key = (String)enumeration.nextElement();
            String value = (String)pro.getProperty(key);
            System.out.println(key+"="+value);
        }
    }
}*/
